import axios from 'axios'
import {ElMessage} from "element-plus"
const NETWORK_ERROR = "网络请求错误，请重新尝试!"

const instance = axios.create()
instance.interceptors.request.use(
     function (config) {
        return config
     },
     function (error) {
        return Promise.reject(error)
     }
)

instance.interceptors.response.use(
    function (response){
        // 正确解构响应数据，从response.data中获取
        const {code,data,msg} = response.data
        if(code == 200) {
            return data
        }
        else{
            ElMessage.error(msg || NETWORK_ERROR)
            return Promise.reject(msg || NETWORK_ERROR)
        }
    }
)

function request (options) {
    options.method = options.method || "get"
    return instance(options)
}

export default request